import * as mobx from 'mobx';
import { WeaForm } from 'comsMobx';
const observable = mobx.observable;
const action = mobx.action;
import * as API_ATTENTION from '../apis/attention';
import { WeaTools ,WeaLocaleProvider} from 'ecCom';
import { ListStore } from './listStore';

import {message} from 'antd';
const getLabel = WeaLocaleProvider.getLabel;
const Base64 = WeaTools.Base64;


export class AttentionStore extends ListStore{

  urlParams = {};//url请求参数
  @observable forceRender = 1;
  topTab = [];//WeaTop tab数据
  @observable tabKey = 0;

  handleParams = {};//批量督办还是单个督办
  @observable showBatchSubmit = false;


  treeParams = {};//点击左侧树存储数据

  //短信
  @observable smsVisible = false;
  @observable smsParams = {src:'supervise',requestid:-1};

  //邮件
  @observable emailVisible = false;
  @observable fwquery = {};
  //关注点击按钮的位置
  @observable clickPotion={};
  @observable followTypeSelectShow = false;
  @observable FollowRequestid='';

  constructor(){
    super();
    this.init = this.init.bind(this);
    this.getSessionkey = this.getSessionkey.bind(this);
    this.getConditionInfo = this.getConditionInfo.bind(this);
    this.getTree = this.getTree.bind(this);
    this.getTreeCount = this.getTreeCount.bind(this);
    this.setByTree = this.setByTree.bind(this);
    this.doSupervise = this.doSupervise.bind(this);
    this.setSmsVisible = this.setSmsVisible.bind(this);
    this.setSmsParams = this.setSmsParams.bind(this);
    this.setShowBatchSubmit = this.setShowBatchSubmit.bind(this);
    this.setEmailVisible = this.setEmailVisible.bind(this);
    this.getSendEmailInfo = this.getSendEmailInfo.bind(this);
    this.reLoad = this.reLoad.bind(this);
    this.clearStatus = this.clearStatus.bind(this);
  }

  @action
  setClickPotion(params) {
      this.clickPotion = params;
  }
  @action('初始化')
  init(urlParams = {}){
    this.urlParams = urlParams;
    if(this.urlParams.tabTitle){
      this.title = Base64.decode(decodeURIComponent(this.urlParams.tabTitle));
    }
    this.getTree();
    this.getTreeCount();
    this.getConditionInfo();
    this.getSessionkey();
  }

  jumpOnlyOnce = true;

   @action jumpTab = (queryParams = {}) => {
     let tabitem = this.topTab.filter( v => v.viewcondition == queryParams.jumpTab);
    if( this.topTab.filter( v => v.viewcondition == queryParams.jumpTab).length > 0 ) {
      this.tabKey = tabitem[0].groupid;
      this.getSessionkey();
    }
 }

  @action('左侧树')
  getTree(params = {}){
    API_ATTENTION.getTree({...params,...this.urlParams}).then((data)=>{
      this.leftTree = data.treedata;
      this.topTab = data.topTab;
      this.forceRender++;
    });
  }


  @action('左侧树统计')
  getTreeCount(params = {}){
    API_ATTENTION.getTreeCount({...params,...this.urlParams}).then((data)=>{
      this.leftTreeCountType = data.countcfg;
      this.leftTreeCount = data.treecount;
      this.forceRender++;
    });
  }


  @action('列表')
  getSessionkey(params={}){
    this.loading = true;
    this.tableStore.setSelectedRowKeys([]);
    const formParams = this.form.getFormParams();
    API_ATTENTION.getSessionkey({...formParams,...params,...this.urlParams,tabKey:this.tabKey,...this.treeParams}).then((data)=>{
      this.tableStore.tableUpdate({datas: []})
      this.tableStore.getDatas(data.sessionkey, params.current || 1);
      if(!this.title){
        this.title = data.title;
      }
      this.topTab = data.topTab;
      if(this.jumpOnlyOnce) {
        this.jumpOnlyOnce = false;
        this.jumpTab(this.urlParams);
      }
      //判断当前tab中是否存在上次打开的那个tab,如果存在，不改变，如果不存在，tab定位到所有，重新请求类表数据
      let result = this.HasTabkey();
      if(!result){
        this.tabKey="flowAll";
        this.getSessionkey({})
      }
      this.loading = false;
    });
  }

  HasTabkey=()=>{
    let result=false;
    if(this.tabKey == 0)
     result=true;
    else{
      this.topTab.map(item=>{
        if( item.groupid == this.tabKey){
          result = true;
        }
       })
    }
    return result;
  }

  //点击【全部类型】
  onSearchLabelClick=()=>{
    this.treeParams = {};
    this.form.resetForm();
    this.getSessionkey();
  }

  @action('高级查询form信息')
  getConditionInfo(params={}){
    this.setShowSearchAd(false);
    API_ATTENTION.getCondition().then((data)=>{
        let condition = data.conditioninfo;
        this.condition = condition;
        this.form.initFormFields(condition);
    });
  }

  @action('点击左侧树初始化表单参数')
  setByTree(key,counts,types){
    let tabKey = 0;
    if (types.name == 'flowOver') {
      tabKey = 1;
    }
    this.tabKey = tabKey;
    if(!key)return;
    let treeParams = {};
    if(key.indexOf("type_") === 0){
      treeParams.workflowType = key.substring(5);
    } else if(key.indexOf("wf_") === 0){
      treeParams.workflowId = key.substring(3);
    }
    this.treeParams = treeParams;
    this.getSessionkey();
  }


  @action('流程督办')
  doSupervise(params = {}){
    message.loading(getLabel(387110,'督办中...，请稍候'),0);
    API_ATTENTION.doSupervise(params).then((data)=>{
      message.destroy();
      if(data.result){
        this.handleParams = {};
        message.success(getLabel(387108,'督办成功'));
        this.reLoad();
      }else{
        message.error(getLabel(387109,'督办失败'));
      }
    });
  }

  @action('刷新table')
  reLoad(params = {}){
    this.getTreeCount();
    this.getSessionkey();
  }

  @action('督办弹框显隐')
  setShowBatchSubmit(showBatchSubmit = false){
    this.showBatchSubmit = showBatchSubmit;
  }

  @action('新建短信关闭打开')
  setSmsVisible(smsVisible = false){
    this.smsVisible = smsVisible;
  }

  @action('新建短信参数设置')
  setSmsParams(smsParams={}){
    this.smsParams = {...this.smsParams,...smsParams};
  }

  @action('发送短信显隐')
  setEmailVisible(emailVisible = false){
    this.emailVisible = emailVisible;
  }

  
  @action('获取邮件催件信息')
  getSendEmailInfo(params={}){
    API_ATTENTION.getSendEmailInfo(params).then((data)=>{
       this.fwquery = data.fwquery || {};
       if(data.result){
        this.setEmailVisible(true);
       }
    });
  }

  @action('清除整个组件的状态')
  clearStatus(){
    this.showSearchAd = false;
    this.selectedTreeKey = "";
    this.clearFormFields();
    this.tabKey = 0;
    this.title = getLabel(503951, '关注事宜');
    this.treeParams = {};
    this.smsVisible = false;
    this.smsParams = {src:'supervise',requestid:-1};
    this.handleParams = {};
    this.emailVisible = false;
    this.fwquery = {};
  }
  clickButttonDrop(key){
    this.tableStore.tableUpdate({checkId:key, selectedRowKeys:[]});
    this.tableStore.reloadCurrent(()=>{
        this.btnObj = {key:key, disabled:true};
        this.operBtnKey = key;
    });
  }
}
